﻿package sparrowGui.item
{
	import flash.events.MouseEvent;
	
	import sparrowGui.Component;
	import sparrowGui.ComponentDraw;
	import sparrowGui.SparrowGUI;
	import sparrowGui.vars.ButtonVars;
	
	/**
	 * baseBtn.as
	 * 基本按钮
	 * 跟SimpleButton的区别是此按钮是通过程序graphics画出来的
	 * 没有文字的，只有图片
	 */
	public class BaseBtn extends ComponentDraw
	{
		private var styVar:ButtonVars;
		protected var _textTag:String;
		
		private var _state:String = MouseEvent.MOUSE_UP;
		
		public function BaseBtn(labelStr:String="Button",vars:ButtonVars=null)
		{
			if(!vars) vars = SparrowGUI.getIns().baseBtnVar;
			styVar = vars;
			
			_textTag = vars.textTag;
			
			super(styVar);
		}

		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			buttonMode = true;
			useHandCursor = true;
			this.mouseChildren = false;
			
			addEventListener(MouseEvent.MOUSE_DOWN, onMouse);
			addEventListener(MouseEvent.MOUSE_OUT, onMouse);
			addEventListener(MouseEvent.ROLL_OVER, onMouse);
		}
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		/**
		 * 鼠标经过按下等事件
		 * @param e
		 */		
		protected function onMouse(e:MouseEvent):void
		{
			switch(e.type){
				case MouseEvent.MOUSE_OUT:
					state = MouseEvent.MOUSE_OUT;
					break;
				case MouseEvent.ROLL_OVER:
					state = MouseEvent.MOUSE_OVER;
					break;
				case MouseEvent.MOUSE_DOWN:
					state = MouseEvent.MOUSE_DOWN;
					stage.addEventListener(MouseEvent.MOUSE_UP, onMouse);
					break;
				case MouseEvent.MOUSE_UP:
					state = MouseEvent.MOUSE_UP;
					stage.removeEventListener(MouseEvent.MOUSE_UP, onMouse);
					break;
			}
		}
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		public function set state(value:String):void
		{
			_state = value;
			switch(_state){
				case MouseEvent.MOUSE_OVER:
					style = styVar.mouseOver;
					break;
				case MouseEvent.MOUSE_DOWN:
					style = styVar.mouseDown;
					break;
				default:
					style = styVar;
					_state = MouseEvent.MOUSE_UP;
					break;
			}
		}
		
		public function get state():String
		{
			return _state;
		}
		
		override public function set width(w:Number):void
		{
			styVar.width = styVar.mouseDown.width = styVar.mouseOver.width = w;
			super.width = w;
		}
		
		override public function set height(h:Number):void
		{
			styVar.height = styVar.mouseDown.height = styVar.mouseOver.height = h;
			super.height = h;
		}
	}
}